package com.toidep.fragment.mains;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.toidep.R;
import com.toidep.activities.MainActivity;
import com.toidep.adapters.FreeAdapter;
import com.toidep.base.BaseFragment;
import com.toidep.config.ConfigUtility;
import com.toidep.config.Constants;
import com.toidep.widgets.sectionlist.AmazingListView;

public class FreeFragment extends BaseFragment {

	private View view;

	private MainActivity mainActivity;
	
	private AmazingListView amazingListView;
	private FreeAdapter adapter;
	
	/**
	 * Set lai cac base value cho fragment: cac item cua thanh header, currentCategory cua menuSideBar
	 */
	public void setBaseValues() {
		mainActivity.currentCategory = Constants.CATEGORY_FREE;
		mainActivity.mainFragment.setTittle(mainActivity.getString(R.string.label_category_free));
		mainActivity.mainFragment.showButtonSlide(true);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		view = inflater.inflate(R.layout.fragment_free, container, false);

		mainActivity = (MainActivity) getActivity();

		setBaseValues();
		
		requestData();
		
		return view;
	}

	public static FreeFragment newInstance() {
		FreeFragment myFragment = new FreeFragment();
		
		myFragment.setRootFragment(true);
		return myFragment;
	}
	
	/**
	 * Goi API get list models
	 */
	private void requestData() {
		// Khoi tao list model New
		amazingListView = (AmazingListView) view.findViewById(R.id.amazingListView);
		amazingListView.setAdapter(adapter = new FreeAdapter(this));
		amazingListView.setLoadingView(getActivity().getLayoutInflater().inflate(R.layout.layout_loading_view, null));
		adapter.setAmazingListView(amazingListView, ConfigUtility.PAGE_SIZE);
		adapter.notifyNoMorePages();
		
		// Chuyen toi detail cua model
		amazingListView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				if (adapter != null && position >= 0) {
					adapter.onItemClick(position);
				}
			}
		});
		adapter.resetList();
	}
	
}
